package com.example.demo1.temp;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author tangming
 */
public class StreamTest {
    List<Person> persons = Arrays.asList(
            new Person(15,"张三", Person.Status.FREE,8888.88),
            new Person(20,"里斯",Person.Status.FREE,2222.88),
            new Person(60,"王武",Person.Status.FREE,345.88),
            new Person(36,"赵六", Person.Status.BUSY,6000.88),
            new Person(36,"赵六", Person.Status.VOCATION,18888.88),
            new Person(40,"陈其", Person.Status.BUSY,9635.88)

    );
    /**案例一：给定一个数字列表，如何返回一个由每个数的平方构成的列表呢？(如：给定【1，2，3，4，5】，返回【1，4，9，16，25】)**/
    @Test
    public void test(){
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        //List<Person> persons = new ArrayList<>();
        boolean b = persons.stream().anyMatch(p -> p.getName().equals("111"));
        list.stream().map(x->x*x).forEach(System.out::println);
        String dept = persons.stream().map(line -> line.getName().split("-")[0])
                .distinct().collect(Collectors.joining("、"));
        System.out.println(dept);
    }
    /** 案例二：**怎样使用 map 和 reduce 数一数流中有多少个 Person 呢？**/
    @Test
    public void test2(){
        Optional<Integer> reduce = persons.stream().map(person -> 1).reduce(Integer::sum);
        System.out.println(reduce.get());
    }
}
